const config = require('./config.js');
const express = require('express');
const bodyParser = require('body-parser');

const app = express();
const indexRouter = require('./router');

const resoucePath = "/";

app.locals.resoucePath = resoucePath;

app.use((req, res, next) => {
    req.start_time = +new Date;
    next();
});

app.use(bodyParser.json());
app.use(bodyParser.urlencoded());


//禁止在前端显示express

app.disable('x-powered-by');

//路由控制
app.use(resoucePath, indexRouter);

// 404异常捕获
app.use(function (req, res, next) {
    const err = new Error('Page Not Found');
    res.json({
        status: 1004,
        desc: err
    });
});


// 500异常捕获
app.use(function (err, req, res, next) {
    err = new Error('Server Error');
    res.json({
        status: 1005,
        desc: err
    });
});

app.listen(config.localServer.port);

console.log("Express server listening at:" + config.localServer.port + " start...");